package org.squirrel.modules.bigscreen.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.dto.ChartDto;
import org.squirrel.modules.bigscreen.dto.ReportDashboardObjectDto;
import org.squirrel.modules.bigscreen.dto.ReportShareDto;
import org.squirrel.modules.bigscreen.entity.ReportDashboard;
import org.squirrel.modules.bigscreen.service.ReportDashboardService;
import org.squirrel.modules.bigscreen.service.ReportShareService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Api(tags = "大屏设计管理")
@RequestMapping("/reportDashboard")
public class ReportDashboardController extends SquirrelController<ReportDashboard, ReportDashboardService> {

	@Autowired
	private ReportDashboardService reportDashboardService;

	@Autowired
	private ReportShareService reportShareService;

	/**
	 * 预览、查询大屏详情
	 * 
	 * @param reportCode
	 * @return
	 */
	@ApiOperation(value = "预览、查询大屏详情", notes = "预览、查询大屏详情")
	@GetMapping({ "/{reportCode}" })
	public Result<?> detail(@PathVariable("reportCode") String reportCode) {
		try {
			ReportDashboardObjectDto reportDashboardObjectDto = reportDashboardService.getDetail(reportCode);

			return Result.OK("预览、查询大屏详情成功", reportDashboardObjectDto);
		} catch (Exception e) {
			log.error("预览、查询大屏详情异常", e);
			return Result.error("预览、查询大屏详情失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 保存大屏设计
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "保存大屏设计", notes = "保存大屏设计")
	@PostMapping("/insert")
	public Result<?> insert(@RequestBody ReportDashboardObjectDto dto) {
		try {
			reportDashboardService.insertDashboard(dto);
			return Result.OK("保存大屏设计成功", dto);
		} catch (Exception e) {
			log.error("保存大屏设计异常", e);
			return Result.error("保存大屏设计失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 获取去单个图层数据
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "查看大屏", notes = "查看大屏")
	@PostMapping("/getData")
	public Result<?> getData(@RequestBody ChartDto dto) {
		try {
			Object obj = reportDashboardService.getChartData(dto);
			return Result.OK("查看大屏成功", obj);
		} catch (Exception e) {
			log.error("查看大屏异常", e);
			return Result.error("查看大屏失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 导出大屏
	 * 
	 * @param reportCode
	 * @return
	 */
	@ApiOperation(value = "导出大屏", notes = "导出大屏")
	@GetMapping("/export")
	public Result<?> exportDashboard(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("reportCode") String reportCode,
			@RequestParam(value = "showDataSet", required = false, defaultValue = "1") Integer showDataSet) {
		try {
			ResponseEntity<byte[]> responseEntity = reportDashboardService.exportDashboard(request, response,
					reportCode, showDataSet);
			return Result.OK("导出大屏成功", responseEntity);
		} catch (Exception e) {
			log.error("导出大屏异常", e);
			return Result.error("导出大屏失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 导入大屏
	 * 
	 * @param file       导入的zip文件
	 * @param reportCode
	 * @return
	 */
	@ApiOperation(value = "导入大屏", notes = "导入大屏")
	@PostMapping("/import/{reportCode}")
	public Result<?> importDashboard(@RequestParam("file") MultipartFile file,
			@PathVariable("reportCode") String reportCode) {
		try {
			reportDashboardService.importDashboard(file, reportCode);
			return Result.OK("导入大屏成功");
		}catch(Exception e) {
			log.error("导入大屏异常", e);
			return Result.error("导入大屏失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 分享
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "分享", notes = "分享")
	@PostMapping("/share")
	public Result<?> share(@Validated @RequestBody ReportShareDto dto) {
		try {
			ReportShareDto  reportShareDto = reportShareService.insertShare(dto);
			return Result.OK("分享成功",reportShareDto);
		}catch(Exception e) {
			log.error("导入大屏异常", e);
			return Result.error("导入大屏失败,原因:" + e.getMessage());
		}
	}

}
